package com.zitangkou.user.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zitangkou.common.enums.RespEnum;
import com.zitangkou.common.enums.StatusEnum;
import com.zitangkou.common.global.GlobalResp;
import com.zitangkou.user.info.PermissionInfo;
import com.zitangkou.user.local.UserThreadLocal;
import com.zitangkou.user.req.permission.AddPermissionReq;
import com.zitangkou.user.req.permission.GetPermissionByPageReq;
import com.zitangkou.user.req.permission.ModifyPermissionReq;
import com.zitangkou.user.resp.permission.PermissionActionResp;
import com.zitangkou.user.service.IPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 权限模块
 *
 * @author Deng Ningning
 * @version 1.0
 * @date 2020/8/31 16:39
 */

@RestController
@RequestMapping("/api/v1/permissions")
public class PermissionController {

    @Autowired
    private IPermissionService permissionService;

    /**
     * 分页查询权限列表
     *
     * @param req
     * @return com.zitangkou.common.global.GlobalResp<com.baomidou.mybatisplus.core.metadata.IPage < com.zitangkou.user.model.info.PermissionInfo>>
     * @date 2021/2/27 11:24
     */
    @GetMapping("/page")
    public GlobalResp<IPage<PermissionInfo>> getPermissionPage(
            @Validated GetPermissionByPageReq req) {
        return GlobalResp.success(permissionService.getPermissionPage(req));
    }

    /**
     * 添加部门
     *
     * @param req
     * @return com.zitangkou.common.global.GlobalResp
     * @author Deng Ningning
     * @date 2020/9/2 11:51
     */
    @PostMapping("/add")
    public GlobalResp<String> addPermission(
            @RequestBody @Validated AddPermissionReq req) {
        return permissionService.addPermission(req, UserThreadLocal.getUserInfo())
                ? GlobalResp.success() : GlobalResp.fail(RespEnum.ADD_PERMISSION_FAIL);
    }

    /**
     * 修改部门
     *
     * @param req
     * @return com.zitangkou.common.global.GlobalResp
     * @author Deng Ningning
     * @date 2020/9/2 11:53
     */
    @PutMapping("/modify")
    public GlobalResp<String> modifyPermission(
            @RequestBody @Validated ModifyPermissionReq req) {
        return permissionService.modifyPermission(req, UserThreadLocal.getUserInfo())
                ? GlobalResp.success() : GlobalResp.fail(RespEnum.MODIFY_PERMISSION_FAIL);
    }

    /**
     * 删除权限
     *
     * @param permissionId
     * @return com.zitangkou.common.global.GlobalResp
     * @author Deng Ningning
     * @date 2020/9/2 11:55
     */
    @DeleteMapping("/remove/{permissionId}")
    public GlobalResp<String> removePermission(
            @PathVariable String permissionId) {
        return permissionService.updatePermissionStatus(permissionId, StatusEnum.DELETED, UserThreadLocal.getUserInfo())
                ? GlobalResp.success() : GlobalResp.fail(RespEnum.DELETE_PERMISSION_FAIL);
    }

    /**
     * 禁用权限
     *
     * @param permissionId
     * @return com.zitangkou.common.global.GlobalResp
     * @author Deng Ningning
     * @date 2020/9/2 11:55
     */
    @DeleteMapping("/disable/{permissionId}")
    public GlobalResp<String> disablePermission(
            @PathVariable String permissionId) {
        return permissionService.updatePermissionStatus(permissionId, StatusEnum.DISABLED, UserThreadLocal.getUserInfo())
                ? GlobalResp.success() : GlobalResp.fail(RespEnum.DISABLE_PERMISSION_FAIL);
    }

    /**
     * 查询部门详情
     *
     * @param permissionId
     * @return com.zitangkou.common.global.GlobalResp<com.zitangkou.user.model.entity.Permission>
     * @author Deng Ningning
     * @date 2020/9/2 11:56
     */
    @GetMapping("/get/{permissionId}")
    public GlobalResp<PermissionInfo> getPermission(
            @PathVariable String permissionId) {
        return GlobalResp.success(permissionService.getPermissionById(permissionId));
    }

    /**
     * 查询权限所有操作列表
     *
     * @return com.zitangkou.common.global.GlobalResp<java.util.List < com.zitangkou.user.model.resp.user.UserLoginActionResp>>
     * @author Deng Ningning
     * @date 2020/12/25 11:33
     */
    @GetMapping("/actions")
    public GlobalResp<List<PermissionActionResp>> getPermissionActions() {
        return GlobalResp.success(permissionService.getPermissionActions());
    }

}
